package com.tools.app.ui.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import com.hm.base.android.ui.AActivity;
import com.tools.ad.ADModule;
import com.tools.ad.EntranceType;
import com.tools.ad.adscene.AdStrategyScene;
import com.tools.ad.adscene.outer.AppOuterScene;
import com.tools.ad.util.GARecordUtils;
import com.tools.app.AppApplication;
import com.tools.app.GAConstants;

import java.lang.ref.WeakReference;

/**
 * author:zhengchaoyi
 * date: 2017/2/27 15:55
 * memo: 1px点的页面，作为提高活跃使用，因为样式问题不再继承于BaseActivity。
 * 这个类用于desktop的report上报和后台8小时的重启任务
 */
public class AppOuterActivity extends AActivity<AppApplication> {

    private static final String TAG = "AppOuterActivity";
    public WeakReference<AppOuterActivity> mWeakReference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createReference();

        Window window = getWindow();
        window.setGravity(Gravity.TOP | Gravity.LEFT);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.x = 0;
        attributes.y = 0;
        attributes.height = 1;
        attributes.width = 1;
        window.setAttributes(attributes);

        GARecordUtils.report(imContext, GAConstants.C_FUN, GAConstants.A_SHOW, "AppOuterActivity");

        ADModule adModule = imContext.getADModule();
        if (adModule != null) {
            AdStrategyScene adScene = adModule.getAdSceneManager().getAdScene(EntranceType.APP_OUTER_AD);
            if (adScene instanceof AppOuterScene) {
                GARecordUtils.report(imContext, GAConstants.C_FUN, GAConstants.A_REQUEST, EntranceType.APP_OUTER_AD.getName());
                ((AppOuterScene) adScene).reload(false);
            }
        }
    }

    private void createReference() {
        mWeakReference = new WeakReference<>(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWeakReference != null && mWeakReference.get() == this) {
            mWeakReference = null;
        }
    }

    @Override
    protected void startMainFrame() {

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        finishSelf();
        return super.dispatchTouchEvent(motionEvent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        finishSelf();
        return super.onTouchEvent(motionEvent);
    }

    public void finishSelf() {
        if (!isFinishing()) {
            finish();
        }
    }

    /**
     * 获取页面交互状态
     *
     * @return
     */
    private boolean isScreenOn() {
        try {
            PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                return powerManager.isInteractive();
            } else {
                if (powerManager != null) {
                    return powerManager.isScreenOn();
                }
            }
        } catch (Exception e) {

        }
        return false;
    }

}
